﻿using FTBS.Models.BLO;
using FTBS.Models.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using PagedList;

namespace FTBS.Controllers
{
    public class FormatController : Controller
    {
        FormatBLO _bloFormat = new FormatBLO();

        /// <summary>
        /// Call View Index and load all formats
        /// </summary>
        /// <returns></returns>
        /// /// <author>hungph</author>
        /// <date>3/3/2014</date>
        public ActionResult Index(string sortOrder, string currentFilter, string searchString, int? page, bool? isSortDescending)
        {
            if (this.Session["RoleName"] == null || this.Session["UserName"] == null || this.Session["Account"] == null || this.Session["RoleName"].ToString().Trim() != "Staff")
            {
                return Redirect("/Home/Announcement");
            }
            List<Format> listFormat = new List<Format>();
            listFormat = _bloFormat.LoadFormats();

            ViewBag.NameSortParm = "formatName_desc";
            ViewBag.CurrentSort = sortOrder;

            if (searchString != null)
            {
                page = 1;
            }
            else
            {
                searchString = currentFilter;
            }
            ViewBag.CurrentFilter = searchString;
            ViewBag.IsSortDes = false;

            if (!String.IsNullOrEmpty(searchString))
            {
                listFormat = listFormat.Where(format => format.FormatName.ToUpper().Contains(searchString.ToUpper())).ToList();
            }

            switch (sortOrder)
            {
                case "formatName_desc":
                    if (isSortDescending == true)
                    {
                        listFormat = listFormat.OrderByDescending(format => format.FormatName).ToList();
                        ViewBag.IsSortDes = false;
                    }
                    else
                    {
                        listFormat = listFormat.OrderBy(format => format.FormatName).ToList();
                        ViewBag.IsSortDes = true;
                    }
                    break;
                default:
                    listFormat = listFormat.OrderBy(format => format.FormatId).ToList();
                    ViewBag.IsSortDes = false;
                    break;
            }

            int pageSize = 5;
            int pageNumber = (page ?? 1);
            return View(listFormat.ToPagedList(pageNumber, pageSize));
        }

        /// <summary>
        /// Call View Create
        /// </summary>
        /// <returns></returns>
        /// <author>hungph</author>
        /// <date>3/3/2014</date>
        public ActionResult Create()
        {
            if (this.Session["RoleName"] == null || this.Session["UserName"] == null || this.Session["Account"] == null || this.Session["RoleName"].ToString().Trim() != "Staff")
            {
                return Redirect("/Home/Announcement");
            }
            ViewBag.formatName = null;
            return View();
        }

        /// <summary>
        /// Create a format film
        /// </summary>
        /// <param name="format"></param>
        /// <returns></returns>
        /// <author>hungph</author>
        /// <date>3/3/2014</date>
        [HttpPost]
        public ActionResult Create(Format format)
        {
            if (this.Session["RoleName"] == null || this.Session["UserName"] == null || this.Session["Account"] == null || this.Session["RoleName"].ToString().Trim() != "Staff")
            {
                return Redirect("/Home/Announcement");
            }
            if (ModelState.IsValid)
            {
                if (!_bloFormat.CheckIsExisted(format))
                {
                    ViewBag.formatName = null;
                    _bloFormat.CreateFormat(format);
                    return RedirectToAction("Index");
                }
                ViewBag.formatName = format.FormatName;
                return View(format);
            }
            if (_bloFormat.CheckIsExisted(format))
            {
                ViewBag.formatName = format.FormatName;
            }
            return View(format);
        }

        /// <summary>
        /// Load object Format by FormatId
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        /// <author>hungph</author>
        /// <date>3/3/2014</date>
        public ActionResult Edit(int id = 0)
        {
            if (this.Session["RoleName"] == null || this.Session["UserName"] == null || this.Session["Account"] == null || this.Session["RoleName"].ToString().Trim() != "Staff")
            {
                return Redirect("/Home/Announcement");
            }
            ViewBag.formatName = null;
            Format format = _bloFormat.LoadFormatsById(id);
            if (format == null)
            {
                return HttpNotFound();
            }
            return View(format);
        }

        /// <summary>
        /// edit info of format film
        /// </summary>
        /// <param name="format"></param>
        /// <returns></returns>
        /// <author>hungph</author>
        /// <date>3/3/2014</date>
        [HttpPost]
        public ActionResult Edit(Format format)
        {
            if (this.Session["RoleName"] == null || this.Session["UserName"] == null || this.Session["Account"] == null || this.Session["RoleName"].ToString().Trim() != "Staff")
            {
                return Redirect("/Home/Announcement");
            }
            var id = format.FormatId;
            if (ModelState.IsValid)
            {
                if (!_bloFormat.CheckIsExisted(format))
                {
                    ViewBag.formatName = null;
                    _bloFormat.EditFormat(format);
                    return RedirectToAction("Index");
                }
                if (format.FormatName.Equals(_bloFormat.GetFormatById(id).FormatName))
                {
                    return RedirectToAction("Index");
                }
                ViewBag.formatName = format.FormatName;
                return View(format);
            }
            ViewBag.formatName = format.FormatName;
            if (_bloFormat.CheckIsExisted(format) && format.FormatName.Equals(_bloFormat.GetFormatById(id).FormatName))
            {
                ViewBag.formatName = null;
            }
            return View(format);
        }

        /// <summary>
        /// delete a format film
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        /// <author>hungph</author>
        /// <date>3/3/2014</date>
        [HttpDelete, ActionName("Delete")]
        public ActionResult Delete(int id = 0)
        {
            if (this.Session["RoleName"] == null || this.Session["UserName"] == null || this.Session["Account"] == null || this.Session["RoleName"].ToString().Trim() != "Staff")
            {
                return Redirect("/Home/Announcement");
            }
            Format format = _bloFormat.LoadFormatsById(id);
            if (format == null)
            {
                return HttpNotFound();
            }
                _bloFormat.DeleteFormat(format);
            return RedirectToAction("Index");
        }
    }
}
